<?php
$caption = BUtils::T('gallery_lbl');
$this->pageTitle = Yii::app()->name . ' - ' . $caption;

$upload_photo_btn = BUtils::T('upload_photo_btn');
?>
<div class="container prepend-top">
	<?php echo CHtml::button('< ' . BUtils::T('back_btn'), array('class'=>'button', 'onclick'=>"history.go(-1);")); ?>
</div>

<div class="container prepend-top append-bottom">
	<div class="span-18">
		<div class="box_white">
		<div class="biz-form">
			<div class="margin">
				<div class="left"><h3><?php echo $caption; ?></h3></div>
				<div class="right">
<?php $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
	'id' => 'uploadFile',
	'postParams' => array('prefix' => 'gal_'),
	'config' => array(
		'buttonName' => $upload_photo_btn,
		'action' => Yii::app()->createUrl('/merchant/uploadGallery'),
		'allowedExtensions'=>array("jpg","jpeg","gif","png"),
		'sizeLimit'=>8*1024*1024,
		'onComplete' => "js:function(id, fileName, responseJSON){ handleUploaded(responseJSON); }",
	),
)); ?>
				</div>
				<div class="clear"></div>
				<div class="tips"><?php echo BUtils::T('gallery_tips_eg'); ?></div>
			</div>
			<div class="">
<?php
if (count($gallery_rows) > 0) {
	echo '<div class="gallery">';
	foreach ($gallery_rows as $r) {
		$urlPicture = str_replace('thumb_', '', $r['url_picture']);
		$urlThumb = $r['url_picture'];
		$comments = Yii::app()->locale->dateFormatter->formatDateTime($r['ts_create_time']);
		$image_close = Yii::app()->baseUrl . '/images/btn-close.png';
		echo <<<HTML
<div class="photo relative" id="{$r['id']}">
	<div class="actions">
		<a class="remove-icon" title="delete this photo?" href="javascript:;" data-id="{$r['id']}" data-url="{$r['url_picture']}"><img src="{$image_close}" /></a>
	</div>
	<div class="thumb-wrap">
		<a href="{$urlPicture}"><img src="{$urlThumb}" alt="" /></a>
	</div>
	<div class="comments">
		<span>{$comments}</span>
	</div>
	<div class="clearfix"></div>
</div>
HTML;
	}
	echo '</div>';
	echo '<div class="clear"></div>';
}
?>
			</div>
		</div><!-- end of <div class="biz-form"> -->
		</div>
	</div>
	
	<div class="span-6 right last">
		<?php BHelper::HtmlCompletionTips(); ?>
	</div>
	
</div>

<div id="dialog-delete" title="<?php echo BUtils::T('confirm_photo_delete_lbl'); ?>" style="display:none;">
	<p><?php echo BUtils::T('confirm_photo_delete_msg'); ?></p>
</div>

<script type="text/javascript">
$(function() {
	//show big photo
	$('.thumb-wrap a').fancybox();
	//show delete button
	$('.photo').mouseenter(function() {
		$(this).addClass('selected').children('.actions').show();
	}).mouseleave(function(){
		$(this).removeClass('selected').children('.actions').hide();
    });
	//delete button action
	$('.remove-icon').click(function() {
		var id = $(this).data('id');
		var url = $(this).data('url');
		$( "#dialog-delete" ).dialog({
			modal: true, resizable: false, draggable: false,
			buttons: {
				<?php echo BUtils::T('cancel_btn'); ?>: function() {
					$(this).dialog("close");
				},
				<?php echo BUtils::T('confirm_btn'); ?>: function() {
					$(this).dialog("close");
					$('#'+id).remove();
					$.get('<?php echo Yii::app()->createUrl('/merchant/deleteGallery'); ?>', {id:id, url:url});
				}
			}
		});
	});
});
function handleUploaded(respJSON) {
	if (respJSON.error) {
		$('.qq-upload-button').html('<span>'+respJSON.error+'</span>');
	} else {
		//console.log(respJSON);
		window.location.reload();
	}
}
</script>